using System;
using System.Collections.Generic;

namespace ICSharpCode.TextEditor.Document
{
    public class JavaFoldingStrategy : IFoldingStrategy
    {
        public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
        {
            return GenerateFoldMarkers(document);
        }

        public List<FoldMarker> GenerateFoldMarkers(IDocument document)
        {
            List<FoldMarker> list = new List<FoldMarker>();
            int num = 0;
            Stack<int> stack = new Stack<int>();
            Stack<string> stack2 = new Stack<string>();
           // int num2 = 0;
            Stack<int> stack3 = new Stack<int>();
            Stack<string> stack4 = new Stack<string>();
            int endLine = 0;
            bool flag = false;
            string text = "";
            bool flag2 = false;
            string text2 = "";
            checked
            {
                for (int i = 0; i < document.TotalNumberOfLines; i++)
                {
                    int textLength = document.TextLength;
                    LineSegment lineSegment = document.GetLineSegment(i);
                    int j;
                    for (j = lineSegment.Offset; j < textLength; j++)
                    {
                        char charAt;
                        if ((charAt = document.GetCharAt(j)) != ' ' && charAt != '\t')
                        {
                            break;
                        }
                    }
                    if (j == textLength)
                    {
                        break;
                    }
                    int num3 = j - lineSegment.Offset;
                    string text3 = document.GetText(document.GetLineSegment(i));
                    if (text3.Trim().StartsWith("import", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!flag)
                        {
                            stack.Push(i);
                            flag = true;
                            text = text3;
                        }
                        endLine = i;
                        stack2.Push(text3);
                    }
                    if (flag && !string.IsNullOrEmpty(text3) && !string.IsNullOrEmpty(text3.Trim()) && !text3.Trim().StartsWith("import", StringComparison.OrdinalIgnoreCase))
                    {
                        num = stack.Pop();
                        string text4 = stack2.Pop();
                        string text5 = "import ...";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text.Length, endLine, num3 + text.Length + text4.Length + text5.Length, FoldType.Unspecified, text5));
                        flag = false;
                    }
                    if (text3.Trim().StartsWith("/**", StringComparison.OrdinalIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text3);
                        flag2 = true;
                    }
                    if (flag2 && text3.Trim().StartsWith("*", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(text2) && !string.IsNullOrEmpty(text3.Replace('*', ' ').Trim()))
                    {
                        text2 = text3.Replace('*', ' ').Trim();
                    }
                    if (text3.Trim().StartsWith("*/", StringComparison.OrdinalIgnoreCase))
                    {
                        if (stack.Count > 0)
                        {
                            num = stack.Pop();
                            string text6 = stack2.Pop();
                            string foldText = text2 + "...";
                            list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text6.TrimStart().Length, i, num3 + text6.Length + text3.Length + "//".Length, FoldType.Unspecified, foldText));
                        }
                        text2 = "";
                        flag2 = false;
                    }
                    if ((text3.Trim().StartsWith("{") || text3.Trim().EndsWith("{")) && (text3.Trim().StartsWith("{") || (text3.Trim().IndexOf("}") == -1 && text3.Trim().EndsWith("{"))))
                    {
                        stack3.Push(i);
                    }
                    if (text3.Trim().StartsWith("}") && stack3.Count > 0)
                    {
                        int num4 = stack3.Pop();
                        list.Add(new FoldMarker(document, num4, document.GetLineSegment(num4).Length - "{".Length, i, 57, FoldType.TypeBody, "{...}"));
                    }
                }
                return list;
            }
        }
    }
}
